package com.ysh.dao.impl;

import com.ysh.dao.UserDao;
import com.ysh.domain.Student;
import com.ysh.domain.User;
import com.ysh.utils.DataSourceUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/*
    持久层操作 JDBCTemplate
 */
public class UserDaoImpl implements UserDao {
    // 创建JdbcTemplate 模板对象
    private JdbcTemplate jt = new JdbcTemplate(DataSourceUtils.getDataSource());


    @Override
    public User findByUsernameAndPwd(String username, String pwd) {
        User u = null;
        try {
            u = jt.queryForObject("select * from t_user where name=? and password=?", new BeanPropertyRowMapper<User>(User.class), username, pwd);
        }catch (Exception e){
            // 未找到会抛异常，进行处理，继续执行
        }
        return u;
    }

    @Override
    public List<Student> findAll() {
        return jt.query("select * from student", new BeanPropertyRowMapper<Student>(Student.class));
    }
}
